package org.itboys.mobile.controller.weixin;

import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.SundayCouponDto;
import org.itboys.mobile.service.mysql.front.SundayFrontCouponService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by zhangyang on 2017/12/20.
 * 圣代_优惠券_controller_weixin
 */
@Controller
@RequestMapping("/sunday/weixin/coupon")
public class SundayWeiXinCouponController extends BaseController{

    @Autowired
    private SundayFrontCouponService couponService;


    /**
     * 优惠券页面
     * @param request
     * @param
     */
    @RequestMapping(value="/index", method=RequestMethod.GET)
    public String couponNotUse(
            HttpServletRequest request,Model model){
        try{
            Long memberId = AccountIdHoldUtils.getSessionMember(request).getId();
            List<SundayCouponDto> list = couponService.select(memberId,null);
            model.addAttribute("list",list);
            return "/aigou/weixin/coupon/index";
        }catch (Exception e){
            logger.error("/sunday/weixin/coupon/index----e="+e.getMessage());
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }


    /**
     * 查询最优优惠券
     * @param request
     * @param response
     * @param amount    金额
     */
   /* @RequestMapping(value="/selectOptimal", method= RequestMethod.POST)
    @ApiOperation(value="查询最优优惠券")
    public void selectOptimal(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required=false, name="amount", value="金额")@RequestParam(value = "amount", required = false)BigDecimal amount){
        try{
            Long memberId = AccountIdHoldUtils.getSessionMember(request).getId();
            SundayCoupon coupon = couponService.getOptimalCoupon(memberId,amount);
            ResponseMessage.success(coupon);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/coupon/selectOptimal----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }*/
}
